var faults=[];//所有故障
var voltage="all";
//{level:faultDTOs}
var windLevelFaultMap=new Map();
var tid;
$(function(){
	tid = $('#tid').val();
	getData(tid);
});
//换电压等级
function onVolChange(){
	voltage=$("#volSelector").val();
	windLevelFaultMap.clear();
	$("#tbTitle").html("");
	if(faultTb){
		faultTb.getTable().destroy();
		$('#faultTb').empty();
		faultTb=null;
	}
	getData();
}
//根据台风id获取相关故障
function getData() {
	if(tid){
		$.getJSON('/rest/TyphoonRestService/getFaultByTyphoonId/' + tid+"/"+voltage,
				function(result) {
			faults = result;
			getChartDatas();
		});
	}
}
function getChartDatas(){
	for(var i in faults){
		var windlevel=faults[i].historyTyphoon.windlevel+"级";
		var faultRecords=windLevelFaultMap.get(windlevel);
		if(!faultRecords){
			faultRecords=[];
		}
		faultRecords.push(faults[i]);
		windLevelFaultMap.put(windlevel,faultRecords);
	}
	//排序
	var dataArr=[];
	for(var i in windLevelFaultMap.keys){
		var level=windLevelFaultMap.keys[i];
		var faultRecords=windLevelFaultMap.get(level);
		dataArr.push([level.replace("级",""),faultRecords.length]);
	}
	dataArr.sort(function(x,y){
		return Number(x[0])-Number(y[0]);
	});
	//组织chart数据
	var maxLevel="";
	var maxValue=0;
	var xAxisData = [];
	var yAxisData =[];
	for(var i in dataArr){
		xAxisData.push(dataArr[i][0]);
		yAxisData.push(dataArr[i][1]);
		if(dataArr[i][1]>maxValue){
			maxValue=dataArr[i][1];
			maxLevel=dataArr[i][0];
		}
	}
	drawChart(xAxisData,yAxisData);
	showTyphoonFault(maxLevel,maxValue);
}
function showTyphoonFault(maxLevel,maxValue) {
	var voltages = {'all':'','499':'500kV及以上','220':'220kV','110':'110kV'};
	var str= "本次台风共发生"+voltages[voltage]+"故障<span class='count'>"+ faults.length + "</span>次";
	if(faults.length>0){
		str+="，其中"+maxLevel+"级风力故障发生次数最多为<span class='count'>"+maxValue+"</span>次。";
	}
	$('#typhoonTitle').html(str);
}
var bar ;
function drawChart(xAxisData,yAxisData){
	bar = new CategoryBar("bar");
	bar.setxAxis(xAxisData);
	bar.setyAxis(yAxisData);
	bar.setxAxisTitle("风力等级");
	bar.setyAxisTitle("故障次数");
	bar.setyAxisName("故障次数");
	var option = bar.getOption();
	bar.show();
	bar.getChart().on("click",function(param){
		var dataIndex=param.dataIndex;
		var level=xAxisData[dataIndex]+"级";
		var faultDTOs=windLevelFaultMap.get(level);
		getTbData(level,faultDTOs);
	});
}
function getTbData(level,faultDTOs){
	var faultMap=new Map();
	for(var i in faultDTOs){
		var faultRecord=faultDTOs[i].faultRecord;
		var faultRecords=faultMap.get(faultRecord.equipment);
		if(!faultRecords){
			faultRecords=[];
		}
		faultRecords.push(faultRecord);
		faultMap.put(faultRecord.equipment,faultRecords);
	}
	var data=[];
	for(var i in faultMap.keys){
		var equipment=faultMap.keys[i];
		var count=faultMap.get(equipment).length;
		data.push([equipment,count]);
	}
	data.sort(function(x,y){
		return y[1]-x[1];
	});
	initTb(level,data);
}
var faultTb;
function initTb(level,data){
	$("#tbTitle").html(level+"风力故障");
	if(!faultTb){
		var opt = MyTable.getDefaultOptions();
		opt.paging = false;
		opt.scrollY = '260px';
		var columns = [ '故障设备', '故障次数'];
		faultTb = new MyTable('faultTb', columns, opt);
		faultTb.setDataSet(data);
		faultTb.show();
	}else{
		faultTb.reload(data);
	}
}
function resizeHtml() {
	if (bar) {
		bar.getChart().resize();
	}
	if (faultTb) {
		faultTb.getTable().draw();
	}
}